package com.masterclient.app.businessmodule.homemodule.view.adapter.promotions;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.banner.BannerNewBean;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.utils.CommonUtil;


public class HomePageXAdGroupAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    private BannerNewBean adGroupDomains;

    private EventAdDataCallback eventAdDataCallback;

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }

    public HomePageXAdGroupAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public HomePageXAdGroupAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, BannerNewBean adGroupDomains) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.adGroupDomains = adGroupDomains;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        if (adGroupDomains.getList().size() == 1) {
            ImageView ivPic = holder.itemView.findViewById(R.id.iv_pic);
            ivPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(0).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(0).getImage(), ivPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
        } else if (adGroupDomains.getList().size() == 2) {
            ImageView ivTopPic = holder.itemView.findViewById(R.id.iv_top_pic);
            ImageView ivBottomPic = holder.itemView.findViewById(R.id.iv_bottom_pic);
            ivTopPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(0).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivBottomPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(1).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(0).getImage(), ivTopPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(1).getImage(), ivBottomPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
        } else if (adGroupDomains.getList().size() == 3) {
            ImageView ivLeftPic = holder.itemView.findViewById(R.id.iv_lef_pic);
            ImageView ivTopPic = holder.itemView.findViewById(R.id.iv_top_pic);
            ImageView ivBottomPic = holder.itemView.findViewById(R.id.iv_bottom_pic);
            ivLeftPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(0).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivTopPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(1).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivBottomPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(2).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(0).getImage(), ivLeftPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(1).getImage(), ivTopPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(2).getImage(), ivBottomPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
        } else if (adGroupDomains.getList().size() == 4) {
            ImageView ivLeftTopPic = holder.itemView.findViewById(R.id.iv_left_top_pic);
            ImageView ivLeftBottomPic = holder.itemView.findViewById(R.id.iv_left_bottom_pic);
            ImageView ivRightTopPic = holder.itemView.findViewById(R.id.iv_right_top_pic);
            ImageView ivRightBottomPic = holder.itemView.findViewById(R.id.iv_right_bottom_pic);
            ivLeftTopPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(0).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivLeftBottomPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(1).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivRightTopPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(2).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivRightBottomPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(3).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(0).getImage(), ivLeftTopPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(1).getImage(), ivRightTopPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(2).getImage(), ivLeftBottomPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(3).getImage(), ivRightBottomPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
        } else if (adGroupDomains.getList().size() == 5) {
            ImageView ivLeftTopPic = holder.itemView.findViewById(R.id.iv_left_top_pic);
            ImageView ivLeftBottomPic = holder.itemView.findViewById(R.id.iv_left_bottom_pic);
            ImageView ivRightTopPic = holder.itemView.findViewById(R.id.iv_right_top_pic);
            ImageView ivRightMiddlePic = holder.itemView.findViewById(R.id.iv_right_middle_pic);
            ImageView ivRightBottomPic = holder.itemView.findViewById(R.id.iv_right_bottom_pic);
            ivLeftTopPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(0).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivLeftBottomPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(1).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivRightTopPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(2).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivRightMiddlePic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(3).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ivRightBottomPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.getList().get(4).getTarget(),adGroupDomains.getList().get(0).getTarget_id()
                            ,"","","","");
                }
            });
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(0).getImage(), ivLeftTopPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(1).getImage(), ivLeftBottomPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(2).getImage(), ivRightTopPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(3).getImage(), ivRightMiddlePic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.getList().get(4).getImage(), ivRightBottomPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
        }
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }
}
